<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML" 4.01"//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Created: August 16, 2001-->
<!--
$Id: SurveyResolve.php,v 1.6 2001/11/02 19:11:34 eugenem Exp $
$Header: /export/home/eugenem/public_html/ComplianceReports/RCS/SurveyResolve.php,v 1.6 2001/11/02 19:11:34 eugenem Exp $
$Log: SurveyResolve.php,v $
Revision 1.6  2001/11/02 19:11:34  eugenem
Moved HTTP_*_VARS processing to dbc.inc

Revision 1.5  2001/11/02 15:00:56  eugenem
Moved database connect statements to a separate file.

Revision 1.4  2001/10/18 15:05:26  eugenem
Record timestamp when resolving a recommendation.

Revision 1.3  2001/09/05 19:49:37  eugenem
Add room description to update status header

Revision 1.2  2001/09/05 18:22:38  eugenem
Changed table structure for Recommendations table.

Revision 1.1  2001/08/16 17:53:30  eugenem
Initial revision

SurveyResolve.php
Entry point for this form is from SurveyResRec.php. Takes an array of
recommendations submitted from SurveyResRec.php and changes the status
to resolved.

If a service report was submitted from SurveyResRec.php, the file is
processed, moved to an appropriate service report folder and the path
to the file is inserted into the database.
-->

<html>
<head>
<title>Radiological Equipment Database</title>
<meta name="author" content="Eugene Mah">
<link rev="made" href="mailto:eugenemah@gmail.com">
<link rev="home" href="http://imablog.net/">
<meta name="description" content="">
<meta name="Keywords" content="">
<meta name="Reply-to" content="eugenemah@gmail.com">
<META http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" l r (SS~~0001))'>
<?PHP
require('functions.inc');
require('dbc.inc');

?>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<?PHP
try {
  $conn = new PDO(
    'mysql:dbname=ComplianceReports;host=localhost',
    'rad','tfFxn2Tley');
}
catch (Exception $e) {
  throw new Exception($e->getMessage());
}

$uploaddir = "ServiceReports/";

// Process the form data submitted by SurveyResRec.php
if (!isset($WONum) || empty($WONum) || $WONum=="") $WONum = "NULL";
if (!isset($ResolvedBy) || empty($ResolvedBy)) $ResolvedBy = "";
if (!isset($RecResolveDate) || empty($RecResolveDate) || $RecResolveDate=="") $RecResolveDate = date('Y-m-d');

// Get the year from $RecResolveDate
$survYr = date_parse($RecResolveDate)['year'];

// Append the year to $uploaddir
$uploaddir .= $survYr . "/";

// Check to see if a file was uploaded
if ($_FILES['ServiceReport']['error'] === UPLOAD_ERR_OK) {
  // Recommendations were resolved with an attached service report.
  $uploadFile = $uploaddir . $SurveyID . "_" . basename($_FILES['ServiceReport']['name']);
  // Check to make sure a file doesn't exist already
  if (file_exists($uploadFile)) {
    echo "<p>Can't upload file. Duplicate file name already exists</p>";
    exit();
  }
  // We only want PDF files. Check and make sure what was uploaded was a PDF
  if (!($finfo = finfo_open(FILEINFO_MIME))) {
    echo "<p>Unable to open magic file database</p>";
    finfo_close($finfo);
    exit();
  }
  $ftype = split(";",finfo_file($finfo, $_FILES['ServiceReport']['tmp_name']));
  if ($ftype[0] == "application/pdf") {
    // Uploaded file is a PDF. Move it to $uploaddir
    if (move_uploaded_file($_FILES['ServiceReport']['tmp_name'], $uploadFile) == false) {
      echo "<p>File upload failed</p>";
      exit();
    }
  }
  else {
    echo "<p>Uploaded file is not a PDF</p>";
    exit();
  }
  // Update the database
  while (list($RecID,$value) = each($Resolved)) {
    $query_updateRecs = $conn->prepare(
      "UPDATE Recommendations SET Resolved=1, RecStatus=\"Complete\", RecResolveTS=now(),
       RecResolveDate=:RecResolveDate, WONum=:WONum,ResolvedBy=:ResolvedBy,
       ServiceReportPath = :uploadFile
       WHERE SurveyID = :SurveyID AND RecID=:value");
    $query_updateRecs->bindParam(':RecResolveDate', $RecResolveDate, PDO::PARAM_STR);
    $query_updateRecs->bindParam(':WONum', $WONum, PDO::PARAM_INT);
    $query_updateRecs->bindParam(':ResolvedBy', $ResolvedBy, PDO::PARAM_STR);
    $query_updateRecs->bindParam(':uploadFile', $uploadFile, PDO::PARAM_STR);
    $query_updateRecs->bindParam(':SurveyID', $SurveyID, PDO::PARAM_INT);
    $query_updateRecs->bindParam(':value', $value, PDO::PARAM_INT);
    $query_updateRecs->execute();
  }
}
else {
  // Recommendations were resolved, but no service report was uploaded
  while (list($RecID,$value) = each($Resolved)) {
    $query_updateRecs = $conn->prepare(
      "UPDATE Recommendations SET Resolved=1, RecStatus=\"Complete\", RecResolveTS=now(),
       RecResolveDate=:RecResolveDate, WONum=:WONum,ResolvedBy=:ResolvedBy
       WHERE SurveyID = :SurveyID AND RecID=:value");
    $query_updateRecs->bindParam(':RecResolveDate', $RecResolveDate, PDO::PARAM_STR);
    $query_updateRecs->bindParam(':WONum', $WONum, PDO::PARAM_INT);
    $query_updateRecs->bindParam(':ResolvedBy', $ResolvedBy, PDO::PARAM_STR);
    $query_updateRecs->bindParam(':SurveyID', $SurveyID, PDO::PARAM_INT);
    $query_updateRecs->bindParam(':value', $value, PDO::PARAM_INT);
    $query_updateRecs->execute();
  }
}

?>
<h2>Recommendations updated for <?PHP echo $Description; ?></h2>
<?PHP
// Get the recommendations
$query_recs = $conn->prepare("SELECT RecID,Recommendation,Resolved,RecAddTS,RecResolveTS,RecResolveDate,ResolvedBy,WONum
               FROM Recommendations
               LEFT JOIN Survey2Machine USING (SurveyID)
               LEFT JOIN Machines USING (MachineID)
               WHERE Recommendations.SurveyID=:SurveyID AND Machines.Status=\"Active\"");
$query_recs->bindParam(':SurveyID', $SurveyID, PDO::PARAM_INT);
$query_recs->execute();
?>

<!-- Output the results into a form -->
<p>Unresolved recommendations are in bold with the checkbox in front</p>
<form action="SurveyResolve.php" method="POST">
<input type="HIDDEN" name="SurveyID" value="<?PHP echo $SurveyID; ?>">
<input type="HIDDEN" name="Description" value="<?PHP echo $Description; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="8000000" />
<table>
  <tr>
    <th>Resolved</th><th>Recommendation</th><th>Date Added</th><th>Date Resolved</th><th>Work Order</th>
  </tr>
<?PHP
  while ($row = $query_recs->fetch(PDO::FETCH_ASSOC)) {
    echo "<tr>\n";
    if ($row['Resolved']) {
      // Recommendation is already resolved.  Print the recommendaiton
      // but skip the checkbox
      echo "<td>&nbsp;</td>\n";
      echo "<td>" . $row['Recommendation'] . "</td>\n";
    }
    else {
      // Recommendation is not resolved yet.  Need a checkbox.
      // $Resolved[i] is passed to the next form only if checked
      echo "<td><input type=\"CHECKBOX\" name=\"Resolved[]\" value=" . $row['RecID'] . "></td>\n";
      echo "<td><b>" . $row['Recommendation'] . "</b></td>\n";
    }
    echo "<td>" . $row['RecAddTS'] . "</td>\n";
    echo "<td>";
    if (isset($row['RecResolveDate'])) echo $row['RecResolveDate'];
    else echo "NA";
    echo "</td>\n";
    if ($row['WONum'] <> "") {
      // If there is a workorder number, check if there is a service report (ServiceReportPath is not null)
      if (isset($row['ServiceReportPath'])) {
        // Link to the service report if there is one
        echo "<td><a href=\"" . $row['ServiceReportPath'] . "\">" . $row['WONum'] . "</a></td>\n";
      }
      else {
        // No service report. Just show the work order number
        echo "<td>" . $row['WONum'] . "</td>\n";
      }
    }
    else {
      echo "<td>&nbsp;</td>\n";
    }
    echo "</tr>\n";
  }
?>

</table>
<p>
Biomed Work Order Number: <input name="WONum" type="text" size="20" maxlength="20" /><br />
Resolution date: <input name="RecResolveDate" type="text" size="20" maxlength="20" /><br />
Upload service report: <input type="file" name="ServiceReport" /><br />
Resolved by: <input name="ResolvedBy" type="text" size="20" maxlength="20" />
</p>
<p><input type="SUBMIT" value="Resolve"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
</form>

<h2>Resolve Recommendations</h2>
<form action="SurveyResRec.php" method="POST">
<p>Select a survey: 
<?PHP
$survID = mysql_query("SELECT DISTINCT Recommendations.SurveyID 
                       FROM Recommendations
                       LEFT JOIN Survey2Machine USING (SurveyID)
                       LEFT JOIN Machines USING (MachineID)
                       WHERE Resolved = 0 AND Machines.Status=\"Active\" ORDER BY Recommendations.SurveyID");
if (empty($SurveyID)) $SurveyID="";
echo listbox_one_field($survID,"SurveyID",$SurveyID);
?>
</p>
<p><input type="SUBMIT" value="Get Survey"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
</form>


<!-- A small menu -->

<?PHP mysql_close(); ?>
</body>
</html>
